package com.ruoyi.web.controller.order;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.Vo.ProductVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.SeasonProduct;
import com.ruoyi.order.service.ISeasonProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 季节推荐Controller
 * 
 * @author ruoyi
 * @date 2022-02-04
 */
@Controller
@RequestMapping("/order/seasonProduct")
public class SeasonProductController extends BaseController
{
    private String prefix = "order/seasonProduct";

    @Autowired
    private ISeasonProductService seasonProductService;

    @RequiresPermissions("order:seasonProduct:view")
    @GetMapping()
    public String seasonProduct()
    {
        return prefix + "/seasonProduct";
    }

    /**
     * 查询季节推荐列表
     */
    @RequiresPermissions("order:seasonProduct:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SeasonProduct seasonProduct)
    {
        startPage();
        List<SeasonProduct> list = seasonProductService.selectSeasonProductList(seasonProduct);
        return getDataTable(list);
    }

    /**
     * 导出季节推荐列表
     */
    @RequiresPermissions("order:seasonProduct:export")
    @Log(title = "季节推荐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SeasonProduct seasonProduct)
    {
        List<SeasonProduct> list = seasonProductService.selectSeasonProductList(seasonProduct);
        ExcelUtil<SeasonProduct> util = new ExcelUtil<SeasonProduct>(SeasonProduct.class);
        return util.exportExcel(list, "季节推荐数据");
    }

    /**
     * 新增季节推荐
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存季节推荐
     */
    @RequiresPermissions("order:seasonProduct:add")
    @Log(title = "季节推荐", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SeasonProduct seasonProduct)
    {
        return toAjax(seasonProductService.insertSeasonProduct(seasonProduct));
    }

    /**
     * 修改季节推荐
     */
    @RequiresPermissions("order:seasonProduct:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        SeasonProduct seasonProduct = seasonProductService.selectSeasonProductById(id);
        mmap.put("seasonProduct", seasonProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存季节推荐
     */
    @RequiresPermissions("order:seasonProduct:edit")
    @Log(title = "季节推荐", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SeasonProduct seasonProduct)
    {
        return toAjax(seasonProductService.updateSeasonProduct(seasonProduct));
    }

    /**
     * 删除季节推荐
     */
    @RequiresPermissions("order:seasonProduct:remove")
    @Log(title = "季节推荐", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)

    {
        return toAjax(seasonProductService.deleteSeasonProductByIds(ids));
    }


    /**
     * 查询热销产品列表信息
     */
    @PostMapping("/getSeasonProductPage")
    @ResponseBody
    public TableDataInfo getSeasonProductPage(String productName )
    {
        startPage();
        List<ProductVo> list = seasonProductService.getSeasonProductPage(productName);
        return getDataTable(list);
    }

    /**
     * 查询热销产品列表信息
     */
    @PostMapping("/getSeasonProductPageList")
    @ResponseBody
    public TableDataInfo getSeasonProductPageList()
    {
        startPage();
        List<ProductVo> list = seasonProductService.getSeasonProductPageList();
        return getDataTable(list);
    }
    /**
     * 查询单个热销产品信息
     */
    @PostMapping("/addSeasonProduct")
    @ResponseBody
    public AjaxResult addSeasonProduct(@RequestParam("productIDs") Integer[]  productIDs)
    {
        int count = 0;
        if (productIDs !=null && productIDs.length > 0 ){
            for (int i = 0; i < productIDs.length; i++) {
                SeasonProduct seasonProduct = new SeasonProduct() ;
                seasonProduct.setProductID(productIDs[i]);
                seasonProduct.setCreateTime(DateUtils.getNowDate());
                count +=seasonProductService.insertSeasonProduct(seasonProduct);
            }
        }
        return toAjax(count);
    }
}
